home *** CD-ROM | disk | FTP | other *** search
- ' this module demonstrate that the Sub New procedure can be used to
- ' initialize module variables
-
- Module MathFunctions
- ' A public constant
- Public Const DOUBLEPI As Double = 6.28318530717958
-
- ' A private array
- Private factValues(169) As Double
-
- Sub New()
- ' Evaluate all possible values in advance.
- Dim i As Integer
- factValues(0) = 1
- For i = 1 To 169
- factValues(i) = factValues(i - 1) * CDbl(i)
- Next
- End Sub
-
- ' Return the Factorial of a number in the range 0-169.
- Function Factorial(ByVal n As Integer) As Double
- ' Check the argument.
- If N >= 0 And N <= 169 Then
- ' Return the value in the array if argument is in range.
- Factorial = factValues(N)
- Else
- ' Raise an error otherwise.
- Err.Raise(6, , "Overflow")
- End If
- End Function
-
- ' --------------------------------------------------------
- ' the following function is the first version of the
- ' Factorial function, that doesn't take advantage of the
- ' Sub New procedure for initializing the factValues array
- ' --------------------------------------------------------
-
- ' Return the Factorial of a number in the range 0-169.
- Public Function Factorial_Old(ByVal n As Integer) As Double
- ' Evaluate all possible values in advance during the first call.
- If factValues(0) = 0 Then
- Dim i As Integer
-
- factValues(0) = 1
- For i = 1 To 169
- factValues(i) = factValues(i - 1) * CDbl(i)
- Next
- End If
-
- ' Check the argument.
- If n >= 0 And n <= 169 Then
- ' Return the value in the array if argument is in range.
- Factorial_Old = factValues(n)
- Else
- ' Raise an error otherwise.
- Err.Raise(6, , "Overflow")
- End If
- End Function
- End Module
-